/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package queenb.web.controller;

import java.util.List;
import javax.faces.event.ActionEvent;
import queenb.net.entitybean.K300;
import queenb.web.store.K300Store;
import queenb.web.util.SessionProvider;
import queenb.web.util.Util;

/**
 *
 * @author Nguyen Hung
 */
public class K300Controller extends QueenbController {

    private int pk300;
    private String kv301;
    private String kv302;
    private String kv303;// phan biet khach hàng
    private int k000;// pahn biet kahch hang
    K300Store k300Store;
    private boolean flagCheck;

    public K300Controller() {
        k300Store = new K300Store();
        Object action = getRequest().getParameter("action");
        Object pk300Object = getRequest().getParameter("pk300");
        if (action != null) {
            if (action.equals("edit")) {
                this.pk300 = Util.parseInt(pk300Object);
                getDetailEditK300(pk300);
            }
        }
    }

    public List<K300> getListAllK300() {

        try {
            if (SessionProvider.getSessionMapValue("K300BOOLEAN") != null) {
                if (SessionProvider.getSessionMapValue("K300BOOLEAN").equals(true)) {
                    kv303 = "C";
                } else {
                    kv303 = "K";
                }
            } else {
                kv303 = "K";
            }
            List<K300> list = k300Store.getListK300All(getLogin().getModule(), getLogin().getCompanyNo(), kv303, getLogin().getUserName());
            return list;
        } catch (Exception ex) {
            System.out.println(ex);
            return null;
        }

    }

    public void getDetailEditK300(int id) {
        try {
            List<K300> listK300Edit = getListAllK300();
            if (listK300Edit.size() > 0) {
                for (int i = 0; i < listK300Edit.size(); i++) {
                    if (listK300Edit.get(i).getPk300() == id) {
                        this.kv301 = listK300Edit.get(i).getKv301();
                        this.kv302 = listK300Edit.get(i).getKv302();
                        this.kv303 = listK300Edit.get(i).getKv303();
                        System.out.println("----------K300------------" + kv303);
                        if (kv303 != null) {
                            if (kv303.equals("K")) {
                                k000 = 1;
                            } else {
                                k000 = 2;
                            }
                        }
                        break;
                    }
                }
            }
        } catch (Exception ex) {
            System.out.println("Error when getDetailEditN850. ERROR CODE: " + ex.getMessage());
        }
    }

    public void changeK300(ActionEvent ev) {
        try {
            SessionProvider.setSessionMapValue("K300BOOLEAN", flagCheck);
            System.out.println("----------K300--------AAAAAAAAA----" + SessionProvider.getSessionMapValue("K300BOOLEAN"));
        } catch (Exception e) {
        }
    }

    public void updateK300() {
        try {
            if (k000 == 1) {
                kv303 = "K";
            } else {
                kv303 = "C";
            }
            int count = k300Store.updateK300(pk300, kv301, kv302, kv303, getLogin().getModule(), getLogin().getCompanyNo(), getLogin().getUserName());
            if (count > 0) {
                showGlobalReport(true, getResourceString("success"));
            } else {
                showGlobalReport(true, getResourceString("error"));
            }


            sendRedirectUrlTab("setting.jsp?tab=header19&lilnkK300=" + Util.convertMD5(getRequest().getRequestURI()));
        } catch (Exception e) {
        }
    }

    /**
     * @return the pk300
     */
    public int getPk300() {
        return pk300;
    }

    /**
     * @param pk300 the pk300 to set
     */
    public void setPk300(int pk300) {
        this.pk300 = pk300;
    }

    /**
     * @return the kv301
     */
    public String getKv301() {
        return kv301;
    }

    /**
     * @param kv301 the kv301 to set
     */
    public void setKv301(String kv301) {
        this.kv301 = kv301;
    }

    /**
     * @return the kv302
     */
    public String getKv302() {
        return kv302;
    }

    /**
     * @param kv302 the kv302 to set
     */
    public void setKv302(String kv302) {
        this.kv302 = kv302;
    }

    /**
     * @return the kv303
     */
    public String getKv303() {
        return kv303;
    }

    /**
     * @param kv303 the kv303 to set
     */
    public void setKv303(String kv303) {
        this.kv303 = kv303;
    }

    /**
     * @return the k000
     */
    public int getK000() {
        return k000;
    }

    /**
     * @param k000 the k000 to set
     */
    public void setK000(int k000) {
        this.k000 = k000;
    }

    /**
     * @return the flagCheck
     */
    public boolean isFlagCheck() {
        return flagCheck;
    }

    /**
     * @param flagCheck the flagCheck to set
     */
    public void setFlagCheck(boolean flagCheck) {
        this.flagCheck = flagCheck;
    }
}
